 package com.oanda.blackberry.view;
 
 import com.oanda.blackberry.restapi.User;
 import com.oanda.blackberry.util.AbstractController;
 import com.oanda.blackberry.util.AbstractViewPanel;
 import com.oanda.blackberry.util.DefaultController;
 import com.oanda.blackberry.util.Model;
 import com.oanda.blackberry.util.PropertyChangeEvent;
 import com.oanda.blackberry.util.log.Logger;
 
 public class ScreenViewController
   implements AbstractViewPanel
 {
   DefaultController controller;
   String request = null;
   String requestedMethod = null;
 
   public ScreenViewController(DefaultController controller) {
     this.controller = controller;
   }
 
   public void modelPropertyChange(PropertyChangeEvent evt)
   {
     if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("onCloseSymbolsView"))) {
       this.controller.propertyChange(new PropertyChangeEvent(this, "showRatesView", null, this.controller));
     }
     else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("onCloseAccountsView"))) {
       if ((this.request != null) && (this.request.length() > 0)) {
         this.controller.propertyChange(new PropertyChangeEvent(this, this.request, null, this.controller));
         this.request = null;
       }
     }
     else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("setRequestMethod"))) {
       if ((evt.getNewValue() != null) && ((evt.getNewValue() instanceof String)))
       {
         this.requestedMethod = ((String)evt.getNewValue());
         Logger.log("setRequestMethod: " + this.requestedMethod);
       }
     }
     else if ((evt.getPropertyName() == null) || (!evt.getPropertyName().equals("setAccount")))
     {
       if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("mainMenuShown"))) {
         if (isDataRequired())
           getRequiredData();
       }
       else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("requestShowOrdersView"))) {
         if (isDataRequired()) {
           getRequiredData();
           return;
         }
 
         this.request = evt.getPropertyName();
         this.controller.propertyChange(new PropertyChangeEvent(this, "showOrdersView", null, this.controller));
       }
       else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("requestShowPositionsView"))) {
         this.request = evt.getPropertyName();
         if (isDataRequired()) {
           getRequiredData();
           return;
         }
         this.controller.propertyChange(new PropertyChangeEvent(this, "showPositionsView", null, this.controller));
       }
       else if ((evt.getPropertyName() == null) || (!evt.getPropertyName().equals("setRateQuote")))
       {
         if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("registration"))) {
           this.controller.propertyChange(new PropertyChangeEvent(this, "showRegistration", this.controller, this.controller));
         }
         else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("sessionToken"))) {
           Logger.log("SVC parsing session token");
           if (((evt.getNewValue() != null) && (((String)evt.getNewValue()).length() == 0)) || (((String)evt.getNewValue()).equals("")))
           {
             this.controller.propertyChange(new PropertyChangeEvent(this, "showLogin", this.controller, this.controller));
           }
           else
           {
             Logger.log("SVC request: length>0");
             Logger.log("SVC request: " + (this.request != null ? this.request : ""));
             Logger.log("SVC requestedMethod: " + (this.requestedMethod != null ? this.requestedMethod : ""));
             if ((this.requestedMethod != null) && (this.requestedMethod.length() > 0)) {
               if (this.requestedMethod.equals("accountList"))
                 this.controller.accountList(Model.getModel().getSessionToken(), Model.getModel().getUser().getUsername());
               else if (this.requestedMethod.equals("accountStatus"))
                 this.controller.accountStatus(Model.getModel().getActiveAccountId(), Model.getModel().getSessionToken());
               else if (this.requestedMethod.equals("logout"))
               {
                 this.controller.propertyChange(new PropertyChangeEvent(this, "requestShowRatesView", this.controller, this.controller));
               }
               this.requestedMethod = null;
               return;
             }
             if ((this.request != null) && (this.request.length() > 0)) {
               this.controller.propertyChange(new PropertyChangeEvent(this, this.request, null, this.controller));
               this.request = null;
               return;
             }
 
             this.controller.propertyChange(new PropertyChangeEvent(this, "requestShowRatesView", this.controller, this.controller));
           }
         }
         else if (evt.getPropertyName().equals("requestShowRatesView")) {
           this.request = evt.getPropertyName();
 
           Logger.log("requestShowRatesView");
           String s = Model.getModel().getRateSymbolsSelected();
           Logger.log("symbols:-" + s + "-");
 
           if ((s == null) || ((s != null) && (s.length() == 0))) {
             this.controller.listSymbols(Model.getModel().getSessionToken());
           }
 
           this.controller.propertyChange(new PropertyChangeEvent(this, "showRatesView", null, this.controller));
         }
         else if (evt.getPropertyName().equals("requestShowTradesView")) {
           this.request = evt.getPropertyName();
 
           if (isDataRequired()) {
             getRequiredData();
             return;
           }
 
           Logger.log("requestTradesView");
           this.controller.propertyChange(new PropertyChangeEvent(this, "showTradesView", null, this.controller));
         }
         else if (evt.getPropertyName().equals("requestShowHistoryView")) {
           this.request = evt.getPropertyName();
 
           if (isDataRequired()) {
             getRequiredData();
             return;
           }
 
           Logger.log("requestHistoryView");
           this.controller.propertyChange(new PropertyChangeEvent(this, "showHistoryView", null, this.controller));
         }
         else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("requestShowAccountDetailsView")))
         {
           this.controller.propertyChange(new PropertyChangeEvent(this, "showAccountDetailsView", null, evt.getNewValue()));
         }
       }
     }
   }
 
   private void getRequiredData() {
     Logger.log("getting required data");
     if ((Model.getModel().getSessionToken() != null) && (Model.getModel().getAccount() == null))
       this.controller.accountList(Model.getModel().getSessionToken(), Model.getModel().getUser().getUsername());
     else if ((Model.getModel().getSessionToken() != null) && (Model.getModel().getActiveAccountId() == null))
       DefaultController.inform("Please select active account!");
     else if ((Model.getModel().getRateSymbolsSelected() == null) || ((Model.getModel().getRateSymbolsSelected() != null) && (Model.getModel().getRateSymbolsSelected().length() == 0)))
     {
       this.controller.listSymbols(Model.getModel().getSessionToken());
     }
     else if ((Model.getModel().getRateQuotes() == null) || ((Model.getModel().getRateQuotes() != null) && (Model.getModel().getRateQuotes().length == 0)))
     {
       this.controller.updateQuote();
     } else if ((Model.getModel().getRateQuotesAll() == null) || ((Model.getModel().getRateQuotesAll() != null) && (Model.getModel().getRateQuotesAll().length == 0)))
     {
       this.controller.updateQuotesAll();
     }
   }
 
   private boolean isDataRequired() {
     if ((Model.getModel().getSessionToken() != null) && (Model.getModel().getAccount() == null))
       return true;
     if ((Model.getModel().getSessionToken() != null) && (Model.getModel().getActiveAccountId() == null))
       return true;
     if ((Model.getModel().getRateSymbolsSelected() == null) || ((Model.getModel().getRateSymbolsSelected() != null) && (Model.getModel().getRateSymbolsSelected().length() == 0)))
     {
       return true;
     }
 
     return (Model.getModel().getRateQuotes() == null) || ((Model.getModel().getRateQuotes() != null) && (Model.getModel().getRateQuotes().length == 0));
   }
}

